import React, {Component} from 'react';
import './index.less';
import {base, toast} from 'nc-lightapp-front';
import deleteRows from '../../request-functions/checkBlackList';
import handleAllpks from "../../container/functions/handleAllpks";

const {NCModal, NCButton, NCTextArea} = base;
const {Header: ModalHeader, Body: ModalBody, Footer: ModalFooter} = NCModal;


class CheckModal extends Component {
    constructor(props) {
        super(props);
        this.state = {checkcause: ''};
        this.beSure = this.beSure.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.onCommentChange = this.onCommentChange.bind(this);
    }

    onCommentChange(v) {
        this.setState({
            checkcause: v
        })
    }

    closeModal() {
        this.props.updateState({
            checkModalVisible: false
        });
        this.setState({
            checkcause: ''
        })
    }

    async beSure() {
        const {pk_psndoc_bad, includeCheckedPsn, page, updateState, cardPagination, getOnePsnInfo} = this.props;
        const {checkcause} = this.state;

        if (pk_psndoc_bad) {
            try {
                let res = await deleteRows({
                    pk_org: this.props.orgVal && this.props.orgVal.refpk,
                    pk_psndoc_bad,
                    checkcause,
                    includeCheckedPsn
                });
                if (res.success) {
                    toast({
                        color: 'success',
                        content: this.props.json['hi6007-000001']/* 国际化处理： 解除成功*/
                    });
                    if (page === 'main') {
                        this.props.getMainTableData();
                    } else {
                        if (includeCheckedPsn) {
                            this.props.getOnePsnInfo();
                        } else {
                            const nextBad = cardPagination.getNextCardPaginationId({
                                id: pk_psndoc_bad,
                                status: 1
                            });
                            handleAllpks.remove(pk_psndoc_bad);
                            if (nextBad) {
                                updateState({
                                    pk_psndoc_bad: nextBad
                                }, () => {
                                    getOnePsnInfo();
                                });
                            } else {
                                updateState({
                                    pk_psndoc_bad: ''
                                }, () => {
                                    getOnePsnInfo();
                                });
                            }
                        }
                    }
                    this.closeModal();
                }
            }
            catch (e) {
            }
        }
    }

    render() {
        const {
            checkModalVisible
        } = this.props;

        return (
            <NCModal
                show={checkModalVisible}
                size="sm"
                backdrop={'static'}
            >
                <ModalHeader closeButton={false}>
                    {this.props.json['hi6007-000003']}{/* 国际化处理： 请填写解除原因*/}
                </ModalHeader>
                <ModalBody>
                    <NCTextArea
                        className=''
                        maxlength={1024}
                        onChange={this.onCommentChange.bind(this)}
                    />
                </ModalBody>
                <ModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.beSure}
                    >
                        {this.props.json['hi6007-000004']}{/* 国际化处理： 确定*/}
                    </NCButton>
                    <NCButton onClick={this.closeModal}>
                        {this.props.json['hi6007-000005']}{/* 国际化处理： 取消*/}
                    </NCButton>
                </ModalFooter>
            </NCModal>
        );
    }
}

export default CheckModal;
